import axios from "axios";
import Qs from 'qs';
import { Message } from 'element-ui';
//axios.defaults.withCredentials = true;
var ajaxObj = {
    //host: 'http://localhost/houqin/public/',
    host: 'http://hqgj.heweixiao.cn/',
    hostUpDir: 'http://hqgj.heweixiao.cn/',
    //apiHost: 'http://localhost/houqin/public/',
    apiHost: 'http://hqgj.heweixiao.cn/',
    token: '',
    uuid: '',
    apiUrl: {
        //设备导出接口
        // exportequipment: 'export_equipment',
        //资产管理批量删除
        piliangequipmentdelete: 'piliang_equipment_delete',
        //批量删除巡检任务
        pilianggotaskdelete: 'piliang_gotask_delete',
        //巡检路线批量
        piliangXGroutedelete: 'piliang_XGroute_delete',
        //批量删除楼层
        piliangfloordelete: 'piliang_floor_delete',
        //批量删除用户管理员
        pilianguserdelete: 'piliang_user_delete',
        //派单手动自动
        dispatchdelete: 'dispatch_delete',
        //巡更任务
        taskList: "gotask_index",
        taskAdd: "gotask_add",
        taskEdit: "gotask_update",
        taskDel: 'gotask_delete/',
        //图片上传
        base64upload: 'base64_upload',
        //巡更路线
        traceList: "XGroute_index",
        traceAdd: "XGroute_add",
        traceEdit: "XGroute_update",
        traceDel: 'XGroute_delete/',
        traceInfo: 'XGroute_read/',
        traceGenErwei: 'create_qrcode',
        tracedelTa: 'gotask_delete/',
        //巡检任务
        dispatchDelete: 'dispatch_delete',
        //汇总报表
        //维修状况 
        summaryDelete: 'summary_delete',
        //导入文档
        importequipment: "import_equipment",
        //巡更人员
        numberList: 'XGuser_list',
        numberInfo: 'XGuser_edit/', //获取信息
        numberAdd: 'XGuser_add',
        numberEdit: 'XGuser_update/',
        numberDel: 'XGuser_delete/',

        //设备管理
        equipmentList: 'equipment_index', //表表
        equipmentInfo: 'equipment_edit/',
        equipmentAdd: 'equipment_add', //添加
        equipmentEdit: 'equipment_update/', //修改
        equipmentDel: 'equipment_delete/', //删除

        // 楼层
        addlou: 'floor_add',
        loulist: 'floor_room_list',
        dellou: 'floor_delete',
        xiulou: 'floor_update',
        louceng: 'get_buildings',

        // 用户
        login: 'login',
        ADMINuser: 'ADMINuser_save',
        ADMINuserlist: "ADMINuser_list",
        ADMINuserupdate: "ADMINuser_update",
        ADMINuserdelete: "ADMINuser_delete",
        alllist: "all_list",
        allsave: "all_save",
        //查询用户详情
        searchuser: "search_user",

        // 维修
        repairlist: "repair_list",
        WXuseradd: "WXuser_add",
        WXuserlist: "WXuser_list",
        WXuserupdate: "WXuser_update",
        WXuserdelete: "WXuser_delete",
        repair_eqlist0: "repair_eq_list/0",
        repair_eqlist1: "repair_eq_list/1",
        senddispatch: "send_dispatch/",

        // 总表
        summarylist: "summary_list",
        exportsummary: "export_summary_list",
    },
    getUrl: function(name) {
        return this.apiUrl[name];
    },
    /**
     * ajax获取数据方法
     * @param param {object}
     *  url: api 地址
     *  method: 'post' or 'get' 选填
     *  data: {object} 数据
     *  success: {function} 成功回调
     *  failure: {function} 失败回调
     */
    ajax: function(param) {
        var method = param.method ? param.method : "get";
        var url = this.apiUrl[param.url];
        if (param.urlConcat) {
            url += param.urlConcat;
        }
        var data = param.data ? param.data : {};
        if (!this.uuid) {
            this.uuid = localStorage.getItem('uid');
        }
        data.uid = this.uuid;
        var obj = {
            method: method,
            url: url,
            baseURL: param.base ? param.base : this.apiHost,
            data: data
        };
        //obj.data.uid = USER_ID;
        //console.log(obj);
        var options = {
            baseURL: obj.baseURL
        };
        if (method == "get") {
            options.params = obj.data;
            axios
                .get(obj.url, options)
                .then(function(response) {
                    if (param.success) {
                        let data = response.data;
                        if (typeof data.state != "undefined") {
                            let state = parseInt(data.state);
                            if (!isNaN(state)) {
                                data.state = state;
                            }
                        }
                        param.success(data);
                    }
                })
                .catch(function(error) {
                    if (param.error) {
                        param.error(error);
                    }
                });
        } else {
            if (param.headers) {
                options.headers = param.headers;
                axios
                    .post(obj.url, obj.data, options)
                    .then(function(response) {
                        if (param.success) {
                            param.success(response.data);
                        }
                    })
                    .catch(function(error) {
                        if (param.error) {
                            param.error(error);
                        }
                    });
            } else {
                options.headers = {
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
                };
                if (obj.data) {
                    obj.data = Qs.stringify(obj.data);
                    //console.log(obj.data)
                }
                axios
                    .post(obj.url, obj.data, options)
                    .then(function(response) {
                        if (param.success) {
                            param.success(response.data);
                        }
                    })
                    .catch(function(error) {
                        if (param.error) {
                            param.error(error);
                        }
                    });
            }

        }
    },
    /**
     * 获取当前网页中url参数
     * @param name 参数名称
     * @param serachstr 如果不传此参数会，获取当前url中的查询参数
     * @returns {string} 返回获取的值，如果没获取到，返回空字符串： ""
     */
    getUrlParam: function(name, serachstr = "") {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        if (!serachstr) {
            serachstr = window.location.search;
        }
        var r = serachstr.substr(1).match(reg); //匹配目标参数
        if (r != null) {
            return decodeURI(r[2]);
        } else {
            return ""; //返回参数值
        }
    },

    /**
     * 提示方法
     * @param msg 消息内容
     * @param cate 类型
     */
    notice: function(msg = '', cate = 1) {
        let type = '';
        switch (cate) {
            case 1:
                type = 'success';
                break;
            case 2:
                type = 'error';
                break;
            case 3:
                type = 'warning';
                break;
            default:
                type = 'info';
                break
        }
        Message({
            message: msg,
            type: type
        })
    }
};
export default ajaxObj;